//
//  SunProductDetailVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/19.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunVirVC.h"
#import "TXHRrettyRuler.h"
#import "SunLoginVC.h"
#import "SunUseCardsVC.h"
#import "SunWebVC.h"
#import "SunImgShows.h"
#import "SunMyOrderVC.h"
#import "JxWebVC.h"
#undef Y
#define Y(y) NY(y)
@interface SunVirVC ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,UITextFieldDelegate,TXHRrettyRulerDelegate>
{
    UILabel *_basePercent;
    UILabel *_addPercent;
    UIView *_barLine;
    UIImageView *_addSignal;
    UIImageView *_bankSafe;
    UILabel *_start;
    UILabel *_days;
    UIView *_progressView;
    UILabel *_progressLable;
    UILabel *_total;
    UILabel *_leave;
    UITableView *_tb;
    UIButton *_pay;
    NSDictionary *_borrowInfo;
    NSDictionary *_borrowDetail;
    NSMutableArray *_sessionArr;
    BOOL _showDetail;
    int _selectNum;
    UIView *_profitView;
    UIView *_detailView;
    NSArray *_recordArr;
    TXHRrettyRuler *_ruler;
    UITextField *_userInput;
    UILabel *_userInputProfit;
    NSMutableArray *_imgArr;
    NSDictionary *_chooseCard;
    UIView *_checkView;
    UIView *_payView;
    Sun *_sunAlert;
    NSTimer *_timer;
    int _time;
    NSString *_tradeNo;
    float _rulerValue;
    int _imgNum1;
    int _imgNum2;
    UILabel *_titleLabel;
}
@end

@implementation SunVirVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUND;
    [self initData];
    [self creatUI];
    [[Sun shareSun]showActivity:self];
    [self firstToHelp];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self requestToBorrowInfo];
    [_ruler scrollToValue:_rulerValue];
    [self requestToUser];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)firstToHelp{
    if (![[NSUserDefaults standardUserDefaults]boolForKey:@"detailhlep"]) {
        UIImageView *img = [[UIImageView alloc]init];
        img.image = [UIImage imageNamed:@"second_detailhelp"];
        [[Sun shareSun]show:img to:[UIApplication sharedApplication].keyWindow shouldReturn:YES];
        [img lt_All:SunEdgesMake(0, 0, 0, 0) save:YES];
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"detailhlep"];
    }
}

- (void)initData{
    _imgArr = [NSMutableArray array];
    _sunAlert = [[Sun alloc]init];
}

- (void)creatProfitView{
    _profitView = [[UIView alloc]init];
    _ruler = [[TXHRrettyRuler alloc]initWithFrame:CGRectMake(0, 0, X(120), Y(150))];
    [_profitView addSubview:_ruler];
    _ruler.rulerDeletate = self;
    [_ruler lt_YcenterTo:nil save:YES];
    [_ruler lt_setLeft:X(18) to:nil save:YES];
    [_ruler lt_setSize:CGSizeMake(X(120), Y(150)) save:YES];
    [_ruler showRulerScrollViewWithCount:500 average:1000 currentValue:0 smallMode:YES];
    UILabel *label1 = [[UILabel alloc]init];
    [_profitView addSubview:label1];
    [label1 lt_setLeft:X(160) to:nil save:YES];
    [label1 lt_setTop:Y(15) to:nil save:YES];
    [label1 lt_setHeight:Y(18) save:YES];
    label1.textColor = GRAY;
    label1.font = [UIFont systemFontOfSize:14];
    label1.text = @"我要投资(元)";
    label1.tag = 789;
    _userInput = [[UITextField alloc]init];
    [_profitView addSubview:_userInput];
    [_userInput lt_setLeft:X(160) to:nil Right:0 to:nil save:YES];
    [_userInput lt_setTop:Y(42) to:nil save:YES];
    _userInput.textColor = RED;
    _userInput.font = [UIFont systemFontOfSize:16];
    _userInput.keyboardType = UIKeyboardTypeNumberPad;
    _userInput.text = [_borrowInfo objectForKey:@"limitMoney"];
    _userInput.enabled = NO;
    _userInput.delegate = self;
    UIView *line = [[UIView alloc]init];
    [_profitView addSubview:line];
    [line lt_YcenterTo:nil save:YES];
    [line lt_setLeft:X(160) to:nil Right:0 to:nil save:YES];
    [line lt_setHeight:0.5 save:YES];
    line.backgroundColor = LINE;
    UILabel *label2 = [[UILabel alloc]init];
    [_profitView addSubview:label2];
    [label2 lt_setLeft:X(160) to:nil save:YES];
    [label2 lt_setTop:Y(15) to:line save:YES];
    [label2 lt_setHeight:Y(18) save:YES];
    label2.textColor = GRAY;
    label2.font = [UIFont systemFontOfSize:14];
    label2.text = @"预计收益(元)";
    _userInputProfit = [[UILabel alloc]init];
    [_profitView addSubview:_userInputProfit];
    [_userInputProfit lt_setLeft:X(160) to:nil Right:0 to:nil save:YES];
    [_userInputProfit lt_setTop:Y(42) to:line save:YES];
    _userInputProfit.textColor = RED;
    _userInputProfit.font = [UIFont systemFontOfSize:16];
    _userInputProfit.text = [_borrowInfo objectForKey:@"interest"];
    [_ruler scrollToValue:[[_borrowInfo objectForKey:@"limitMoney"] doubleValue]];
    [_ruler unAble];
}

- (void)creatdetailView{
    float left = X(20);
    float margin = X(4);
    _detailView = [[UIView alloc]init];
    UIView *bg1 = [[UIView alloc]init];
    [_detailView addSubview:bg1];
    [bg1 lt_Top:SunEdgesMake(0, 0, 0, Y(8)) to:nil save:YES];
    bg1.backgroundColor = BACKGROUND;
    UIView *line1 = [[UIView alloc]init];
    [_detailView addSubview:line1];
    [line1 lt_Top:SunEdgesMake(0, 0, 0, 0.5) to:bg1 save:YES];
    line1.backgroundColor = LINE;
    UIImageView *img1 = [[UIImageView alloc]init];
    [_detailView addSubview:img1];
    [img1 lt_setSize:CGSizeMake(X(20), Y(20)) save:YES];
    [img1 lt_setTop:Y(13) to:line1 save:YES];
    [img1 lt_setLeft:X(20) to:nil save:YES];
    img1.image = [UIImage imageNamed:@"second_detail"];
    UILabel *l1 = [[UILabel alloc]init];
    [_detailView addSubview:l1];
    [l1 lt_YcenterTo:img1 save:YES];
    [l1 lt_setLeft:X(6) to:img1 save:YES];
    l1.textColor = BLUE;
    l1.font = [UIFont systemFontOfSize:15];
    l1.text = @"项目描述";
    UIView *line2 = [Sun copyWithView:line1];
    [_detailView addSubview:line2];
    [line2 lt_Top:SunEdgesMake(Y(13), 0, 0, 0.5) to:img1 save:YES];
    UILabel *content1 = [[UILabel alloc]init];
    [_detailView addSubview:content1];
    [content1 lt_setLeft:X(20) to:nil Right:X(20) to:nil save:YES];
    [content1 lt_setTop:Y(15) to:line2 save:YES];
    content1.numberOfLines = 0;
    content1.preferredMaxLayoutWidth = SCREENWIDTH - X(40);
    content1.textColor = BLACK;
    content1.font = [UIFont systemFontOfSize:15];
    NSString *string = [_borrowInfo objectForKey:@"borrowItem"];
    string = [Sun htmlFormat:string];
    string  = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    content1.text = string;
    [Sun labelSetLineHeight:4 To:content1];
    
    [_detailView layoutIfNeeded];
    _detailView.frame = CGRectMake(0, 0, 0, content1.frame.origin.y + content1.frame.size.height + Y(15));
    [[Sun shareSun]hideAll];
    int section = _sessionArr.count == 4 ? 2 : 1;
    [_tb reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
    [_tb scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

- (void)creatUI{
    UIView *head = [[UIView alloc]init];
    [self.view addSubview:head];
    [head lt_Top:SunEdgesMake(0, 0, 0, 64) to:nil save:YES];
    head.backgroundColor = BLUE;
    UIView *bar = [[UIView alloc]init];
    [head addSubview:bar];
    [bar lt_Top:SunEdgesMake(20, 0, 0, 44) to:nil save:YES];
    UIButton *returnBtn = [[UIButton alloc]init];
    [bar addSubview:returnBtn];
    [returnBtn lt_YcenterTo:nil save:YES];
    [returnBtn lt_setLeft:X(6) to:nil save:YES];
    [returnBtn lt_setSize:CGSizeMake(X(45), Y(40)) save:YES];
    [returnBtn setImage:[UIImage imageNamed:@"tool_back"] forState:UIControlStateNormal];
    [returnBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    _titleLabel = [[UILabel alloc]init];
    [bar addSubview:_titleLabel];
    [_titleLabel lt_XYcenterTo:nil save:YES];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = WHITE;
    _titleLabel.font = [UIFont systemFontOfSize:18];
    _titleLabel.text = self.title;
    NSArray *cons = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[view(<=%f)]",X(310)] options:0 metrics:nil views:@{@"view":_titleLabel}];
    [bar addConstraints:cons];
    //    UIButton *share = [[UIButton alloc]init];
    //    [bar addSubview:share];
    //    [share lt_YcenterTo:nil save:YES];
    //    [share lt_setRight:X(15) to:nil save:YES];
    //    [share setImage:[UIImage imageNamed:@"second_share"] forState:UIControlStateNormal];
    //    [share addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    _barLine = [[UIView alloc]init];
    [bar addSubview:_barLine];
    [_barLine lt_Bottom:SunEdgesMake(0, 0, 0, 0.5) to:nil save:YES];
    _barLine.backgroundColor = [UIColor grayColor];
    _barLine.hidden = YES;
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, Y(188))];
    [self.view addSubview:bgView];
    bgView.backgroundColor = BLUE;
    UILabel *label1 = [[UILabel alloc]init];
    [bgView addSubview:label1];
    [label1 lt_XcenterTo:nil save:YES];
    [label1 lt_setTop:Y(8) to:nil save:YES];
    [label1 lt_setHeight:Y(13) save:YES];
    label1.font = [UIFont systemFontOfSize:12];
    label1.textColor = WHITE;
    label1.alpha = 0.5;
    label1.text = @"预期年化收益率";
    _bankSafe = [[UIImageView alloc]init];
    [bgView addSubview:_bankSafe];
    [_bankSafe lt_setTop:Y(0) to:nil save:YES];
    [_bankSafe lt_setRight:X(15) to:nil save:YES];
    [_bankSafe lt_setSize:CGSizeMake(X(85), X(85)) save:YES];
    _bankSafe.image = [UIImage imageNamed:@"second_bankSafe"];
    UIView *center = [[UIView alloc]init];
    [bgView addSubview:center];
    _basePercent = [[UILabel alloc]init];
    [center addSubview:_basePercent];
    [_basePercent lt_setHeight:Y(36) save:YES];
    [_basePercent lt_YcenterTo:center save:YES];
    _basePercent.textColor = WHITE;
    [_basePercent setMarkup:@"<font size=\"40\">0.00</font><font size=\"18\">%</font>"];
    _addPercent = [[UILabel alloc]init];
    [center addSubview:_addPercent];
    [_addPercent lt_setHeight:Y(18) save:YES];
    [_addPercent lt_bottomEqual:_basePercent save:YES];
    [_addPercent lt_setLeft:0 to:_basePercent save:YES];
    _addPercent.textColor = [HXColor colorWithHexString:@"fffd59"];
    _addPercent.font = [UIFont systemFontOfSize:18];
    _addPercent.text = @"+0.00%";
    [center lt_leftEqual:_basePercent save:YES];
    [center lt_rightEqual:_addPercent save:YES];
    [center lt_setHeight:Y(36) save:YES];
    [center lt_XcenterTo:nil save:YES];
    [center lt_setTop:Y(10) to:label1 save:YES];
    _addSignal = [[UIImageView alloc]init];
    [center addSubview:_addSignal];
    _addSignal.image = [UIImage imageNamed:@"percent_bg_yellow"];
    [_addSignal lt_setBottom:Y(3) to:_addPercent save:YES];
    [_addSignal lt_setLeft:X(15) to:_basePercent save:YES];
    UILabel *label2 = [[UILabel alloc]init];
    [bgView addSubview:label2];
    [label2 lt_setTop:Y(14) to:center save:YES];
    [label2 lt_setLeft:X(56) to:nil save:YES];
    [label2 lt_setHeight:Y(13) save:YES];
    label2.font = [UIFont systemFontOfSize:12];
    label2.textColor = WHITE;
    label2.alpha = 0.5;
    label2.text = @"起投金额";
    _start = [[UILabel alloc]init];
    [bgView addSubview:_start];
    [_start lt_XcenterTo:label2 save:YES];
    [_start lt_setTop:Y(6) to:label2 save:YES];
    [_start lt_setHeight:Y(19) save:YES];
    _start.textColor = WHITE;
    _start.font = [UIFont systemFontOfSize:19];
    _start.text = @"0元";
    UILabel *label3 = [[UILabel alloc]init];
    [bgView addSubview:label3];
    [label3 lt_bottomEqual:label2 save:YES];
    [label3 lt_setRight:X(56) to:nil save:YES];
    [label3 lt_setHeight:Y(13) save:YES];
    label3.font = [UIFont systemFontOfSize:12];
    label3.textColor = WHITE;
    label3.alpha = 0.5;
    label3.text = @"投资期限";
    _days = [[UILabel alloc]init];
    [bgView addSubview:_days];
    [_days lt_XcenterTo:label3 save:YES];
    [_days lt_bottomEqual:_start save:YES];
    [_days lt_setHeight:Y(19) save:YES];
    _days.textColor = WHITE;
    _days.font = [UIFont systemFontOfSize:19];
    _days.text = @"0天";
    _progressView = [[UIView alloc]init];
    [bgView addSubview:_progressView];
    [_progressView lt_setLeft:X(15)+6 to:nil Right:X(47) to:nil save:YES];
    [_progressView lt_setTop:Y(16) to:_days save:YES];
    [_progressView lt_setHeight:6 save:YES];
    _progressView.backgroundColor = WHITE;
    _progressView.layer.cornerRadius = 3;
    UIView *progressBox = [[UIView alloc]init];
    [_progressView addSubview:progressBox];
    progressBox.tag = 1;
    progressBox.layer.cornerRadius = 3;
    progressBox.backgroundColor = [HXColor colorWithHexString:@"3C96E4"];
    [progressBox lt_Left:SunEdgesMake(0, 0, 0, 0) to:nil save:YES];
    UIView *progressPoint = [[UIView alloc]init];
    [_progressView addSubview:progressPoint];
    [progressPoint lt_setSize:CGSizeMake(16, 16) save:YES];
    [progressPoint lt_YcenterTo:nil save:YES];
    [progressPoint lt_layoutView:progressPoint side:SunCenterX to:progressBox toSide:SunRight withSize:1 offset:0 save:YES];
    progressPoint.backgroundColor = RED;
    progressPoint.layer.borderWidth = 2;
    progressPoint.layer.borderColor = WHITE.CGColor;
    progressPoint.layer.cornerRadius = 8;
    progressPoint.clipsToBounds = YES;
    _progressLable = [[UILabel alloc]init];
    [bgView addSubview:_progressLable];
    [_progressLable lt_YcenterTo:_progressView save:YES];
    [_progressLable lt_setLeft:X(12) to:_progressView save:YES];
    _progressLable.font = [UIFont systemFontOfSize:10];
    _progressLable.textColor = WHITE;
    _progressLable.text = @"0%";
    _total = [[UILabel alloc]init];
    [bgView addSubview:_total];
    [_total lt_setTop:Y(12) to:_progressView save:YES];
    [_total lt_setLeft:X(15) to:nil save:YES];
    _total.textColor = WHITE;
    _total.font = [UIFont systemFontOfSize:13];
    _total.text = @"融资金额：0元";
    _leave = [[UILabel alloc]init];
    [bgView addSubview:_leave];
    [_leave lt_bottomEqual:_total save:YES];
    [_leave lt_setRight:X(15) to:nil save:YES];
    _leave.textColor = WHITE;
    _leave.font = [UIFont systemFontOfSize:13];
    _leave.text = @"剩余可投：0元";
    _tb = [[UITableView alloc]init];
    [self.view addSubview:_tb];
    [_tb lt_setLeft:0 to:nil Right:0 to:nil save:YES];
    [_tb lt_setTop:64 to:nil Bottom:Y(62) to:nil save:YES];
    _tb.backgroundColor = BACKGROUND;
    _tb.delegate = self;
    _tb.dataSource = self;
    _tb.showsVerticalScrollIndicator = NO;
    _tb.tableHeaderView = bgView;
    _tb.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *line = [[UIView alloc]init];
    [self.view addSubview:line];
    [line lt_Bottom:SunEdgesMake(0, Y(62), 0, 1) to:nil save:YES];
    line.backgroundColor = [HXColor colorWithHexString:@"dedede"];
    _pay = [[UIButton alloc]init];
    [self.view addSubview:_pay];
    [_pay lt_Bottom:SunEdgesMake(Y(15), X(10), X(15), Y(44)) to:nil save:YES];
    _pay.clipsToBounds = YES;
    [_pay setBackgroundColor:BLUE forState:UIControlStateNormal];
    [_pay setBackgroundColor:[HXColor colorWithHexString:@"87CEFA"] forState:UIControlStateHighlighted];
    _pay.layer.cornerRadius = 4;
    [_pay setTitle:@"立即投资" forState:UIControlStateNormal];
    [_pay setTitleColor:WHITE forState:UIControlStateNormal];
    _pay.titleLabel.font = [UIFont systemFontOfSize:17];
    [_pay addTarget:self action:@selector(gotoPay) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark function

- (void)showImgs:(UITapGestureRecognizer *)tap{
    SunImgShows *vc = [[SunImgShows alloc]init];
    vc.imgs = _imgArr;
    int index = 0;
    if (tap.view.tag / 1000 == 1) {
        index = (int)tap.view.tag - 1000;
    }
    if (tap.view.tag / 1000 == 2) {
        index = (int)tap.view.tag - 2000 + _imgNum1;
    }
    if (tap.view.tag / 1000 == 3) {
        index = (int)tap.view.tag - 3000 + _imgNum1 + _imgNum2;
    }
    vc.currentIndex = index;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setProgress:(float)progress{
    _progressLable.text = [NSString stringWithFormat:@"%.0f%%",progress * 100];
    UIView *progressBox = [_progressView viewWithTag:1];
    UIView *progressPoint = [_progressView viewWithTag:2];
    [progressBox lt_removeAllCons];
    [progressPoint lt_removeAllCons];
    [UIView animateWithDuration:0.5 animations:^{
        [progressBox lt_Left:SunEdgesMake(0, 0, 0, progressBox.superview.bounds.size.width * progress) to:nil save:YES];
        [progressPoint lt_layoutView:progressPoint side:SunCenterX to:progressBox toSide:SunRight withSize:1 offset:0 save:YES];
        [self.view layoutIfNeeded];
    }];
}

- (void)updateHead{
    if (!_borrowInfo) {
        return;
    }
    NSString *leave = [NSString stringWithFormat:@"%@",[_borrowInfo objectForKey:@"leaveMoney"]];
    _titleLabel.text = [_borrowInfo objectForKey:@"name"];
    _leave.text = [NSString stringWithFormat:@"剩余可投：%@元",[Sun formatStringToMoney:leave]];
    _total.text = [NSString stringWithFormat:@"融资金额：%@元",[Sun formatStringToMoney:[_borrowInfo objectForKey:@"totalMoney"]]];
    [_basePercent setMarkup:[NSString stringWithFormat:@"<font size=\"40\">%.2f</font><font size=\"18\">%%</font>",[[_borrowInfo objectForKey:@"aprBase"]doubleValue]]];
    _days.text = [NSString stringWithFormat:@"%@天",[_borrowInfo objectForKey:@"days"]];
    [self setProgress:[[_borrowInfo objectForKey:@"progress"]doubleValue]];
    _start.text = [NSString stringWithFormat:@"%@元",[Sun formatStringToMoney:[_borrowInfo objectForKey:@"limitMoney"]]];
    if ([[_borrowInfo objectForKey:@"status"] intValue]>0) {
        [_pay setTitle:@"已满标请选择其它项目" forState:UIControlStateNormal];
        _pay.enabled = NO;
    }
    if ([[_borrowInfo objectForKey:@"isTender"]intValue] == 0) {
        [_pay setTitle:@"只限新手用户" forState:UIControlStateNormal];
        _pay.enabled = NO;
    }
    _addPercent.text = @"";
    _addPercent.hidden = YES;
    _addSignal.hidden = YES;
}

#pragma mark calculate
- (float)getProfit:(float)money{
    float count = [[_borrowInfo objectForKey:@"days"]intValue];
    if ([[_borrowInfo objectForKey:@"time"] isEqualToString:@"天"]) {
        count = count/30;
    }
    float x = ([[_borrowInfo objectForKey:@"aprBase"]doubleValue] + [[_borrowInfo objectForKey:@"aprAdd"]doubleValue]) / 100.0;
    float value = money * x * count / 12.0;
    float result = floor(value * 100) / 100.0;
    return result;
}

- (int)getTotalMoney:(float)profit{
    float count = [[_borrowInfo objectForKey:@"days"]intValue];
    if ([[_borrowInfo objectForKey:@"time"] isEqualToString:@"天"]) {
        count = count/30;
    }
    float x = ([[_borrowInfo objectForKey:@"aprBase"]doubleValue] + [[_borrowInfo objectForKey:@"aprAdd"]doubleValue]) / 100.0;
    float result = profit / x / count * 12.0;
    if (result - (int)result >= 0.5) {
        return (int)result + 1;
    }else {
        return (int)result;
    }
}

#pragma mark button


- (void)hideAlert{
    [_sunAlert hideAll];
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)share{
    
}

- (void)gotoPay{
    NSDictionary *dic = [Sun getUser:USER];
    if (![[NSUserDefaults standardUserDefaults] boolForKey:KLOGIN]) {
        SunLoginVC *vc = [[SunLoginVC alloc]init];
        vc.isWeb = NO;
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }else if ([[dic objectForKey:@"status"] intValue] != 3) {
        [SunSupport shouldAlert:self code:UserOpen];
        return;
    }else if ([[dic objectForKey:@"status"] intValue] != 3) {
        [SunSupport shouldAlert:self code:UserBind];
        return;
    }else if ([[dic objectForKey:@"isActivate"] intValue] == 1) {
        [SunSupport shouldAlert:self code:UserActivate];
    }else{
        [[Sun shareSun]showActivity:self];
        NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_VERTENDER];
        NSDictionary *params = @{@"borrowId":[_borrowInfo objectForKey:@"borrowId"]};
        [HttpRequest postURL:url withParam:params success:^(id requestDic, NSString *msg) {
            [[Sun shareSun]alertAlign:NSTextAlignmentCenter title:@"投资成功" content:@"到期后体验标收益，自动转入余额中。" left:@"知道了" right:nil cancel:^{
                [self.navigationController popViewControllerAnimated:YES];
            } sure:nil To:self];
        } failure:^(NSString *errorInfo, int code) {
            [[Sun shareSun]hideActivity];
            [[Sun shareSun]showMsg:errorInfo];
        }];
    }
}

- (void)changeDetail:(UIButton *)btn{
    if (_selectNum == (int)btn.tag - 1) {
        return;
    }
    _selectNum = (int)btn.tag - 1;
    if (_selectNum == 1) {
        if (!_detailView) {
            [self creatdetailView];
            return;
        }
    }
    if (_selectNum == 2) {
        [self requestToBorrowRecord];
        if (!_recordArr) {
            [[Sun shareSun]showActivity:self];
            return;
        }
    }
    
    int section = _sessionArr.count == 4 ? 2 : 1;
    [_tb reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    [_tb scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

#pragma mark request
- (void)requestToUser{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_USERINFO];
    [HttpRequest postURL:url withParam:nil success:^(id requestDic, NSString *msg) {
        [Sun setUser:requestDic forKey:USER];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]showMsg:errorInfo];
    }];
}
- (void)requestToBorrowRecord {
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_VERRECORD];
    NSDictionary *param = @{@"borrowId":[_borrowInfo objectForKey:@"borrowId"]};
    [HttpRequest postURL:url withParam:param success:^(id requestDic, NSString *msg) {
        [[Sun shareSun]hideActivity];
        _recordArr = requestDic;
        int section = 1;
        [_tb reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
        [_tb scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]hideActivity];
        [[Sun shareSun]showMsg:errorInfo];
    }];
}


- (void)requestToBorrowInfo{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_VER];
    [HttpRequest postURL:url withParam:nil success:^(id requestDic, NSString *msg) {
        [[Sun shareSun]hideActivity];
        _borrowInfo = requestDic;
        _sessionArr = [NSMutableArray arrayWithArray:@[@[@{@"title":@"还款方式",@"content":@"一次性还本付息"},@{@"title":@"起息方式",@"content":@"T(满标)+0"},@{@"title":@"募集时间",@"content":[_borrowInfo objectForKey:@"schedule"]}],@""]];
        [self updateHead];
        if (!_profitView){
            [self creatProfitView];
        }
        [_tb reloadData];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]hideActivity];
        [[Sun shareSun]showMsg:errorInfo];
    }];
}




#pragma mark ruler
- (void)txhRrettyRuler:(TXHRulerScrollView *)rulerScrollView{
    float max = [[_borrowInfo objectForKey:@"totalMoney"] doubleValue];
    [_ruler setMaxValue:max];
    _userInput.text = [_borrowInfo objectForKey:@"limitMoney"];
    _userInputProfit.text = [_borrowInfo objectForKey:@"interest"];
    _rulerValue = [[_borrowInfo objectForKey:@"limitMoney"] doubleValue];
}

#pragma mark textField
- (void)textFieldDidEndEditing:(UITextField *)textField{
    _ruler.rulerDeletate = self;
}



#pragma mark scrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y > 0) {
        _barLine.hidden = NO;
    }else{
        _barLine.hidden = YES;
    }
}

#pragma mark tableView
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return Y(50);
    }else{
        return 0.01;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section <= 1) {
        return Y(8);
    }else{
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1){
        if (_selectNum == 0) {
            return Y(155);
        }
        if (_selectNum == 1) {
            return _detailView.bounds.size.height;
        }
        if (_selectNum == 2) {
            if (_recordArr.count == 0 && indexPath.row == 1){
                return SCREENHEIGHT - 64 - Y(100) - Y(62);
            }else {
                return Y(40);
            }
        }
    }
    return Y(50);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _sessionArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        if (_showDetail) {
            return [_sessionArr[section] count];
        }else{
            return 1;
        }
    }else if (section == 1){
        if (_selectNum == 2) {
            if (_recordArr.count == 0) {
                return 2;
            }else {
                return _recordArr.count + 1;
            }
        }else {
            return 1;
        }
    }else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    while (cell.subviews.count > 0) {
        for (int i=0; i<cell.subviews.count; i++) {
            [cell.subviews[i] removeFromSuperview];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIView *line = [[UIView alloc]init];
    [cell addSubview:line];
    line.backgroundColor = LINE;
    [line lt_Bottom:SunEdgesMake(0, 0, 0, 0.5) to:nil save:YES];
    if (indexPath.row == 0) {
        UIView *line1 = [[UIView alloc]init];
        [cell addSubview:line1];
        line1.backgroundColor = LINE;
        [line1 lt_Top:SunEdgesMake(0, 0, 0, 0.5) to:nil save:YES];
    }
    if (indexPath.section == 0) {
        UILabel *label = [[UILabel alloc]init];
        [cell addSubview:label];
        [label lt_YcenterTo:nil save:YES];
        [label lt_setLeft:X(15) to:nil save:YES];
        label.textColor = BLACK;
        label.font = [UIFont systemFontOfSize:15];
        label.text = [_sessionArr[indexPath.section][indexPath.row] objectForKey:@"title"];
        UILabel *content = [[UILabel alloc]init];
        [cell addSubview:content];
        [content lt_YcenterTo:nil save:YES];
        [content lt_setLeft:X(116) to:nil save:YES];
        [content lt_setWidth:X(194) save:YES];
        content.textColor = GRAY;
        content.font = [UIFont systemFontOfSize:15];
        content.text = [_sessionArr[indexPath.section][indexPath.row] objectForKey:@"content"];
        content.numberOfLines = 0;
        if (indexPath.row == 3) {
            content.textColor = BLUE;
        }else {
            content.textColor = GRAY;
        }
        if (indexPath.row == 0) {
            cell.selectionStyle = UITableViewCellSelectionStyleDefault;
            UIImageView *img = [[UIImageView alloc]init];
            [cell addSubview:img];
            [img lt_YcenterTo:nil save:YES];
            [img lt_setRight:X(12) to:nil save:YES];
            UILabel *label2 = [[UILabel alloc]init];
            [cell addSubview:label2];
            [label2 lt_YcenterTo:nil save:YES];
            [label2 lt_setRight:X(28) to:nil save:YES];
            label2.textColor = BLUE;
            label2.font = [UIFont systemFontOfSize:15];
            if (_showDetail) {
                label2.text = @"收起";
                img.image = [UIImage imageNamed:@"second_up"];
            }else{
                label2.text = @"展开";
                img.image = [UIImage imageNamed:@"second_down"];
            }
        }
    }//还款详情
    if (indexPath.section == 1) {
        if (_selectNum == 0) {
            [cell addSubview:_profitView];
            [_profitView lt_All:SunEdgesMake(0, 0, 0, 0) save:YES];
        }
        if (_selectNum == 1) {
            [cell addSubview:_detailView];
            [_detailView lt_All:SunEdgesMake(0, 0, 0, 0) save:YES];
        }
        if (_selectNum == 2) {
            if (indexPath.row == 0) {
                UILabel *l1 = [[UILabel alloc]init];
                [cell addSubview:l1];
                l1.textColor = BLACK;
                l1.font = [UIFont systemFontOfSize:14];
                [l1 lt_YcenterTo:nil save:YES];
                [l1 lt_setLeft:X(42) to:nil save:YES];
                l1.text = @"用户";
                UILabel *l2 = [Sun copyWithView:l1];
                [cell addSubview:l2];
                [l2 lt_YcenterTo:nil save:YES];
                [l2 lt_setLeft:X(150) to:nil save:YES];
                l2.text = @"金额(元)";
                UILabel *l3 = [Sun copyWithView:l1];
                [cell addSubview:l3];
                [l3 lt_YcenterTo:nil save:YES];
                [l3 lt_setLeft:X(265) to:nil save:YES];
                l3.text = @"时间";
            }else {
                if (_recordArr.count == 0) {
                    UIView *view = [[UIView alloc]init];
                    view.backgroundColor = BACKGROUND;
                    [cell addSubview:view];
                    [view lt_All:SunEdgesMake(0, 0, 0, 0) save:YES];
                    UIImageView *img = [[UIImageView alloc]init];
                    [view addSubview:img];
                    [img lt_XcenterTo:nil save:YES];
                    img.image = [UIImage imageNamed:@"third_order_blank"];
                    UILabel *label = [[UILabel alloc]init];
                    [view addSubview:label];
                    [label lt_XcenterTo:nil save:YES];
                    [label lt_setTop:Y(18) to:img save:YES];
                    [label lt_layoutView:label side:SunCenterY to:nil toSide:SunCenterY withSize:1 offset:Y(80) save:YES];
                    label.textColor = GRAY;
                    label.font = [UIFont systemFontOfSize:19];
                    label.text = @"虚位以待！";
                }else {
                    NSDictionary *data = _recordArr[indexPath.row - 1];
                    //                    UIImageView *img = [[UIImageView alloc]init];
                    //                    [cell addSubview:img];
                    //                    [img lt_YcenterTo:nil save:YES];
                    //                    [img lt_setLeft:X(18) to:nil save:YES];
                    //                    if ([data objectForKey:@""]) {
                    //                        img.image = [UIImage imageNamed:@"second_pc"];
                    //                    }else {
                    //                        img.image = [UIImage imageNamed:@"second_mb"];
                    //                    }
                    UILabel *l1 = [[UILabel alloc]init];
                    [cell addSubview:l1];
                    [l1 lt_YcenterTo:nil save:YES];
                    [l1 lt_setLeft:X(30) to:nil save:YES];
                    l1.font = [UIFont systemFontOfSize:13];
                    l1.textColor = RED;
                    l1.text = [data objectForKey:@"phone"];
                    UILabel *l2 = [Sun copyWithView:l1];
                    [cell addSubview:l2];
                    [l2 lt_YcenterTo:nil save:YES];
                    [l2 lt_setLeft:X(155) to:nil save:YES];
                    l2.text = [data objectForKey:@"money"];
                    UILabel *l3 = [Sun copyWithView:l1];
                    [cell addSubview:l3];
                    [l3 lt_YcenterTo:nil save:YES];
                    [l3 lt_setLeft:X(255) to:nil save:YES];
                    l3.textColor = GRAY;
                    l3.text = [Sun timestamp:[[data objectForKey:@"addtime"] intValue] ToTime:@"YY/MM/dd HH:mm"];
                }
            }
        }
    }//标详情
    if ((_sessionArr.count == 4 && indexPath.section == 3) || (_sessionArr.count == 3 && indexPath.section == 2)) {
        UILabel *label = [[UILabel alloc]init];
        [cell addSubview:label];
        [label lt_YcenterTo:nil save:YES];
        [label lt_setLeft:X(15) to:nil save:YES];
        label.textColor = BLACK;
        label.font = [UIFont systemFontOfSize:15];
        label.text = [_sessionArr[indexPath.section] objectForKey:@"title"];
        UIImageView *img = [[UIImageView alloc]init];
        [cell addSubview:img];
        [img lt_YcenterTo:nil save:YES];
        [img lt_setRight:X(12) to:nil save:YES];
        UILabel *label2 = [[UILabel alloc]init];
        [cell addSubview:label2];
        [label2 lt_YcenterTo:nil save:YES];
        [label2 lt_setRight:X(28) to:nil save:YES];
        label2.textColor = BLUE;
        label2.font = [UIFont systemFontOfSize:15];
        img.image = [UIImage imageNamed:@"second_right"];
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
        
    }//卡券
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = WHITE;
        NSArray *arr = @[@"收益估计",@"项目详细",@"投资记录"];
        for (int i=0; i<3; i++) {
            UIButton *btn = [[UIButton alloc]init];
            [view addSubview:btn];
            btn.tag = i + 1;
            [btn setTitle:arr[i] forState:UIControlStateNormal];
            [btn setTitleColor:BLACK forState:UIControlStateNormal];
            [btn setTitleColor:BLUE forState:UIControlStateSelected];
            [btn addTarget:self action:@selector(changeDetail:) forControlEvents:UIControlEventTouchUpInside];
            btn.titleLabel.font = [UIFont systemFontOfSize:15];
            [btn lt_setTop:0 to:nil Bottom:0 to:nil save:YES];
            [btn lt_setWidth:SCREENWIDTH / 3 save:YES];
            [btn lt_setLeft:SCREENWIDTH / 3 * i to:nil save:YES];
            if (_selectNum == i) {
                btn.selected = YES;
                UIView *slide = [[UIView alloc]init];
                [btn addSubview:slide];
                CGSize size =[arr[i] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
                [slide lt_XcenterTo:nil save:YES];
                [slide lt_setSize:CGSizeMake(size.width, Y(3)) save:YES];
                [slide lt_setBottom:0 to:nil save:YES];
                slide.backgroundColor = BLUE;
            }
        }
        UIView *line = [[UIView alloc]init];
        [view addSubview:line];
        line.backgroundColor = LINE;
        [line lt_Bottom:SunEdgesMake(0, 0, 0, 0.5) to:nil save:YES];
        UIView *line2 = [Sun copyWithView:line];
        [view addSubview:line2];
        [line2 lt_Top:SunEdgesMake(0, 0, 0, 0.5) to:nil save:YES];
        return view;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = BACKGROUND;
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            _showDetail = !_showDetail;
            [_tb reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }//还款详情
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
